//
//  BasicDialog.m
//  PigMarket
//
//  Created by Felix on 2019/3/6.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "BasicDialog.h"

@interface BasicDialog ()

/**
 内容View
 */
@property (nonatomic , strong) UIView *basicContentView;

@end

@implementation BasicDialog

#pragma mark - init method
- (instancetype)init
{
    self = [super init];
    if (self) {
        [self initView];
        [self initLayout];
    }
    return self;
}

- (void)initView {
    // 添加子控件
    self.basicContentView = [self getContentView];
    UIWindow *mainWindow = [UIApplication sharedApplication].keyWindow;
    [mainWindow addSubview:self];
    [self addSubview:self.basicContentView];
}

- (void)initLayout {
    // 布局子控件
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    
    [self.basicContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.centerY.mas_equalTo(-20);
        make.width.mas_equalTo(SCREEN_WIDTH * 0.85);
    }];
}

- (UIView *)getContentView {
    return [[UIView alloc] init];
}

- (void)show {
    self.backgroundColor = [UIColor colorWithHexString:@"#000000" alpha:0];
    [UIView beginAnimations:@"fadeIn" context:nil];
    [UIView setAnimationDuration:0.35];
    self.backgroundColor = [UIColor colorWithHexString:@"#000000" alpha:0.6];
    [UIView commitAnimations];
    
    [self showContentViewAnimation];
}

- (void)hide {
    self.basicContentView.hidden = YES;
    [self hideContentViewAnimation];
}

- (void)showContentViewAnimation {
    CAKeyframeAnimation * animation;
    animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    animation.duration = 0.30;
    animation.removedOnCompletion = YES;
    animation.fillMode = kCAFillModeForwards;
    NSMutableArray *values = [NSMutableArray array];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9, 0.9, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.1, 1.1, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
    animation.values = values;
    [self.basicContentView.layer addAnimation:animation forKey:nil];
}

- (void)hideContentViewAnimation {
    [UIView beginAnimations:@"fadeIn" context:nil];
    [UIView setAnimationDuration:0.3];
    self.alpha = 0.0;
    [UIView commitAnimations];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self removeFromSuperview];
    });
    
}

#pragma mark - getter and setter

@end
